package com.barney.support.web.tag;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author Barney Kim
 */
public class ErrorTag extends TagSupport {

	private static final long serialVersionUID = 9142433850751708318L;

	/** 스택 트레이스 출력 여부 */
	private boolean stackTrace = false;

	// 속성 -------------------------------------------------------------

	/**
	 * 스택 트레이스 출력 여부를 설정한다.
	 */
	public void setStackTrace(boolean stackTrace) {
		this.stackTrace = stackTrace;
	}

	/**
	 * 태그 처리
	 */
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		HttpServletRequest request =
			(HttpServletRequest)pageContext.getRequest();
		String requestUri = (String)request.getAttribute(
				"javax.servlet.error.request_uri");
		try {
			if(requestUri != null) {
				out.println("Problem accessing "+requestUri);
			}
			if(stackTrace) {
				Throwable throwable =
					(Throwable)request.getAttribute(
							"javax.servlet.error.exception");
				if(throwable != null) {
					out.println("StackTrace:");
					throwable.printStackTrace(new PrintWriter(out));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (SKIP_BODY);
	}

}
